import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.*;
import net.sf.jasperreports.engine.export.*;
import net.sf.jasperreports.engine.util.*;
import net.sf.jasperreports.view.*;
import ar.com.fdvs.dj.domain.ImageBanner;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.constants.Font;
import ar.com.fdvs.dj.domain.constants.Border;
import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;
import java.awt.Color;


import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.output.*;


class ReportController {

    def index = {

        def rowStyle = new Style()
        rowStyle.setFont(Font.ARIAL_SMALL)
        rowStyle.setBorderBottom(Border.PEN_1_POINT)
        rowStyle.setHorizontalAlign(HorizontalAlign.LEFT)
        rowStyle.setVerticalAlign(VerticalAlign.MIDDLE)

        def headerStyle = new Style()
        headerStyle.setFont(Font.ARIAL_SMALL_BOLD)
        //headerStyle.setBackgroundColor(Color.BLACK)
        headerStyle.setTextColor(Color.BLACK)
        headerStyle.setBorderBottom(Border.PEN_2_POINT)
        headerStyle.setBorderTop(Border.PEN_2_POINT)
        headerStyle.setHorizontalAlign(HorizontalAlign.LEFT)
        headerStyle.setVerticalAlign(VerticalAlign.MIDDLE)


        //addColumn(String title, String property, Class clazz, int width, Style style, Style headerStyle)
        def drb = new FastReportBuilder()
        Integer marginLR = new Integer(50)
        Integer marginTB = new Integer(65)
        drb.setLeftMargin(marginLR).setRightMargin(marginLR).setTopMargin(marginTB).setBottomMargin(marginTB)
        drb.setPrintBackgroundOnOddRows(true)
        drb = drb.addColumn("Nº Inscrição","inscricao",String.class.getName(),30,rowStyle,headerStyle)
        drb = drb.addColumn("Código CNES","cnes",Integer.class.getName(),20,rowStyle,headerStyle)
        drb = drb.addColumn("CNPJ","cnpj",String.class.getName(),20,rowStyle,headerStyle)
        drb = drb.addColumn("Endereço","endereco",String.class.getName(),70,rowStyle,headerStyle)
        drb = drb.addColumn("Modalidade de Atendimento","modalidadeAtendimento",String.class.getName(),70,rowStyle,headerStyle)
        drb = drb.addColumn("Situação","status",String.class.getName(),70,rowStyle,headerStyle)
        drb.addFirstPageImageBanner("http://172.16.220.15:8080/contratualizacao/images/pmf_logo.gif",
            new Integer(100),
            new Integer(25),
            ImageBanner.ALIGN_LEFT)
        DynamicReport dr = drb.setTitle("Secretaria Municipal de Saúde de Fortaleza").setUseFullPageWidth(true).build()
        dr.setSubtitle("Relatório de todos os estabelecimentos de saúde inscritos no processo de contratualização do ano de 2010.")


        def ds = new JRBeanCollectionDataSource(Inscricao.list())
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds)


        def reportFormat = 'PDF'
        def reportFileName = "inscricoes"
        ReportWriter reportWriter = ReportWriterFactory.getInstance().getReportWriter(jp, reportFormat, [(JRHtmlExporterParameter.IMAGES_URI): "${request.contextPath}/report/image?image=".toString()])
        if (reportFileName) {
            response.addHeader('content-disposition', "attachment; filename=${reportFileName}.${reportFormat.toLowerCase()}")
        }
        reportWriter.writeTo(response)
    }

}
